package com.ui_componet.citychoose;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.ui_componet.R;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.List;


/**
 *
 *
 *城市联动选择器
 */
public class CityChooseWheelView extends LinearLayout {
    private WheelView provinceWheel;
    private WheelView cityWheel;
    private WheelView areaWheel;
    private String province;
    private String city;
    private String area;
    private String cityCode;
    private OnDataChangeListener datachangeListener;
    private int DEFAULT_PRIVINCE_INDEX = 0;
    private Context mContext;

    public Context getmContext() {
        return mContext;
    }

    public void setDatachangeListener(OnDataChangeListener datachangeListener) {
        this.datachangeListener = datachangeListener;
    }

    public CityChooseWheelView(Context context) {
        super(context);
        init(context);
    }

    public CityChooseWheelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void setIndex(String province,String city,String area){

    }

    /**
     * 做一系列的初始化
     * */
    private void init(Context context) {
        mContext = context;
        initView(context);
        initData(context);
        initSelector();
    }

    /**
     * 监听省市区变化后的改动
     * */
    private void initSelector() {
        //省
        provinceWheel.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {

            @Override
            public void onItemSelected(int position, Object text) {
                province = (String) text;
                city = getCitys(text.toString()).get(0);
                area = getCountys(city).get(0);
                cityCode=getCityCode(area);
                cityWheel.resetDataFromTop(getCitys(((String) text).toLowerCase()));
                areaWheel.resetDataFromTop(getCountys(city));
                cityWheel.setSelection(0);
                areaWheel.setSelection(0);
                if(datachangeListener != null) {
                    datachangeListener.changeListener(province,city,area,cityCode);
                }
            }
        });
        //市
        cityWheel.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int id, Object text) {
                city = text.toString();
                area = getCountys(city).get(0);
                cityCode=getCityCode(area);
                areaWheel.resetDataFromTop(getCountys(city));
                areaWheel.setSelection(0);
                if(datachangeListener != null) {
                    datachangeListener.changeListener(province,city,area,cityCode);
                }
            }

        });
        //区
        areaWheel.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int id, Object text) {
                area = text.toString();
                cityCode=getCityCode(area);
                if(datachangeListener != null) {
                    datachangeListener.changeListener(province,city,area,cityCode);
                }
            }
        });

    }

    /**
     * 初始化数据
     * */
    private void initData(Context context) {
        province = setWheelData(provinceWheel,getProvinces(),DEFAULT_PRIVINCE_INDEX);
        //城市选择器配置
        city = setWheelData(cityWheel,getCitys(province),DEFAULT_PRIVINCE_INDEX);
        //区选择器配置
        area = setWheelData(areaWheel,getCountys(city),DEFAULT_PRIVINCE_INDEX);
    }

    /**
     * 初始化省，市，区，城市联动选择器
     * */
    private void initView(Context context) {
        View view = View.inflate(context, R.layout.city_choose_wheel,this);
        provinceWheel = (WheelView) view.findViewById(R.id.province_wheel);
        cityWheel = (WheelView) view.findViewById(R.id.city_wheel);
        areaWheel = (WheelView) view.findViewById(R.id.area_wheel);

        initWheel(provinceWheel);
        initWheel(cityWheel);
        initWheel(areaWheel);
    }

    /**
     * 初始化wheelview
     * @param wheelView
     */
    private void initWheel(WheelView wheelView) {
        wheelView.setSelection(0);
        wheelView.setLoop(false);
        wheelView.setWheelSize(17);
        wheelView.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wheelView.setSkin(WheelView.Skin.Common);
    }

    private ArrayList<String> getProvinces() {
        return CityDataModule.getProvinceList(mContext);
    }

    /**
     * 获取市选择器数据源
     */
    private ArrayList<String> getCitys(String province) {
        return CityDataModule.getCityList(mContext,province);
    }
    /**
     * 获取市选择器数据源
     */
    private String getCityCode(String area) {
        return CityDataModule.getAreaCode(mContext,area);
    }

    /**
     * 获取区选择器数据源
     */
    private ArrayList<String> getCountys(String city) {
        return CityDataModule.getAreaListNo(mContext,city);
    }

    private String setWheelData(WheelView wheelView,ArrayList<String> dataList ,int defaultIndex){
        wheelView.setWheelData(dataList);
        wheelView.setSelection(defaultIndex);
        return  dataList.get(defaultIndex);
    }

    public String setInitData(String cityCode){
        List<Integer>  mlist=CityDataModule.getIndex(mContext,cityCode);
        province = setWheelData(provinceWheel,getProvinces(),mlist.get(0));
        //城市选择器配置
        city = setWheelData(cityWheel,getCitys(province),mlist.get(1));
        //区选择器配置
        area = setWheelData(areaWheel,getCountys(city),mlist.get(2));
        return province+city+area;
    }

    public interface OnDataChangeListener{
        void changeListener(String province, String city, String area, String cityCode);
    }

}
